package com.xrjsj.stage.interceptor;

import com.xrjsj.stage.constant.CacheConstant;
import com.xrjsj.stage.util.StrRedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xurong
 * @since 2018/10/1 下午3:05
 */
@Component
public class CommonInterceptorConfig implements HandlerInterceptor {

    @Autowired
    protected StrRedisUtils redisUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        Cookie[] cookies = request.getCookies();
        if(null == cookies){
            return false;
        }
//        for(Cookie cookie : cookies){
//            if(CacheConstant.JOKER_TOKEN.equals(cookie.getName())){
//                String value = redisUtils.get(cookie.getValue());
//                if (value != null) {
//                    String[] keyArr = value.split("-"+CacheConstant.JOKER_TOKEN+"-");
//                    if (keyArr.length > 0) {
//                        coreCross(request,response);
//                        return true;
//                    }
//                }
//
//            }
//
//        }
//        return false;
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)  {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

    }

    private void coreCross(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
    }

}
